﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.IO;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Contact : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            //{
            try
            {
                MasterPage mt = this.Master as MasterPage;
                TextBox uc = mt.FindControl("txtTK") as TextBox;
                uc.Attributes.Add("Width", "150px;");
            }
            catch { }
                int id = 11;
                cmsCategoryDO objCat = new cmsCategoryDO();
                objCat.CategoryID = id;
                objCat = new cmsCategoryBL().Select(objCat);
                Page.Title = new sysConfigBL().SelectByPK(1).Rows[0]["ConfigValue"].ToString();

                //MasterPage pg = this.Master as MasterPage;
                //Control uc = pg.FindControl("ucmnu") as Control;
                //uc.Visible = false;

                lblInfo.Text = objCat.DetailContent;
                //if(!File.Exists("/Media/" + objCat.ImageURL))
                //    imgContent.ImageUrl = "/Media/" + objCat.ImageURL;
                //else
                //    picHolder.Visible = false;
                //hplImg.NavigateUrl = "/Media/" + objCat.ImageURL;
            //}
        }
        public void sendsMail( String name, String bd, string title)
        {
            string i = DateTime.Now.ToString();
            sysConfigDO cmsConfig = new sysConfigDO();
            int id = 4;

            cmsConfig = new sysConfigBL().Select(new sysConfigDO { ConfigID = 4 });

            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential("web@ses.com", "@123456ses");
            SmtpServer.Port = 587;
            SmtpServer.Host = "Smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();
            String addr = cmsConfig.ConfigValue.ToString();
            
            try
            {
                mail.From = new MailAddress(addr, name, System.Text.Encoding.UTF8);
                mail.To.Add(addr);
                mail.Subject = title;
                mail.Body = bd;
                mail.IsBodyHtml = true;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                SmtpServer.Send(mail);
                Response.Write("<script>alert('Send email successful " + i + ";!!!')</script>");

            }
            catch (Exception ex)
            {
                lbLoi.Text = ex.ToString() + addr;
            }
        }
        protected void ButtonSend_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtbv.Text);
            if (!ccJoin.UserValidated)
            {
                Response.Write("<script language='javascript'>alert('Mã bảo vệ chưa chính xác..!!..')</script>");
                return;
            }
            else
            {

                try
                {
                    String ten = txtTen.Text;
                    String bd = txtND.Text;
                    String title = txtSub.Text;
                    sendsMail( ten, bd, title);
                }
                catch (Exception ee)
                {
                    lbLoi.Text = ee.ToString();
                }
            }
        }
    }
}
